package com.lsf.dunlian.netty;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;


import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.lsf.dunlian.Myappliction;
import com.lsf.dunlian.callback.GroupChatCallBack;
import com.lsf.dunlian.config.NettyConfig;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.entity.MessageEntity;
import com.lsf.dunlian.entity.MessageVo;
import com.lsf.dunlian.enums.MsgTypeEnum;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.AttributeKey;

/**
 * 处理服务端返回的数据
 *
 * @author Administrator
 *
 */
public class SimpleClientHandler extends ChannelInboundHandlerAdapter {

    private static final List<GroupChatCallBack> chatCallBacks =new ArrayList<>();
    private Activity activity;
    public SimpleClientHandler(Activity activity){
        this.activity=activity;
    }

    public static void register(GroupChatCallBack chatCallBack){
        chatCallBacks.add(chatCallBack);
    }


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("服务器端返回的数据:" + msg);
        MessageVo vo = new Gson().fromJson(String.valueOf(msg), MessageVo.class);


//        if (MsgTypeEnum.HEARTBEAT.getName().equals(vo.type)) {
//            MessageEntity entity = new MessageEntity();
//            entity.createUserId = PreferenceUtil.getSharedPreference(Constants.Login.PARAM_USERID);
//            entity.msgType = MsgTypeEnum.HEARTBEAT.getName();
//            Myappliction.channel.writeAndFlush(JSONObject.toJSONString(entity) + NettyConfig.DATA_PACK_SEPARATOR);
//        }


        if (MsgTypeEnum.LOGIN.getName().equals(vo.type)) {
//            PreferenceUtil.putSharedPreference(Constants.Login.PARAM_HEADIMG,vo.data.getCreateUserIcon());
        }

        if (MsgTypeEnum.MESSAGE.getName().equals(vo.type)) {
            for (GroupChatCallBack chatCallBack:chatCallBacks) {
                activity.runOnUiThread(() -> chatCallBack.message(vo.data));
            }
        }
    }

    /**
     * 播放通知声音
     */
    public  void playRingTone() {
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone rt = RingtoneManager.getRingtone(activity, uri);
        rt.play();
    }



    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent){
            //写空闲超过5秒就发送一个心跳包
            if(((IdleStateEvent) evt).state() == IdleState.WRITER_IDLE) {
                MessageEntity entity = new MessageEntity();
                entity.createUserId= PreferenceUtil.getSharedPreference(Constant.USERID);
                entity.msgType="heartbeat";
                Myappliction.channel.writeAndFlush(JSONObject.toJSONString(entity) + NettyConfig.DATA_PACK_SEPARATOR);
            }

            //读操作超过10秒就发起登录 表示客户端10秒都没有反馈消息
            if(((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
                Myappliction.netty();
            }
        }else {
            super.userEventTriggered(ctx, evt);
        }
    }

}